﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LY.Entity;
using LY.Entity.SearchEntity;
using LY.Entity.PageSearch;

namespace LY.DataAccess.Repository
{
    public class SystemRepository : BaseRepository
    {

        public PagedList<S_User> QueryUserList(UserSearch search, PageView view)
        {

            string cols = @" [ID]
      ,[UnitID]
      ,[UserName]
      ,[LoginName]
      ,[Password]
      ,[DeptID]
      ,[DeptName]
      ,[SLDeptID]
      ,[SLDeptName]
      ,[LeaderID]
      ,[Sequence]
      ,[IsStop]
      ,[IP]
      ,[Port]
      ,[OnLineStatus]
      ,[ErrCount]
      ,[PwdUpdate]
      ,[Email] ";
            string sqlWhere = "";
            if (!string.IsNullOrEmpty(search.Name))
            {
                sqlWhere += " AND (UserName LIKE '%" + search.Name + "%' OR LoginName LIKE '%" + search.Name + "%')";
            }
            if (search.DeptID.HasValue)
            {
                sqlWhere += " AND DeptID=" + search.DeptID;
            }

            return base.PageGet<S_User>(view, cols,
                @"[S_User] "
                , sqlWhere
                , "ID DESC ",
                "");
        }

         //用户实体
        public S_User GetUser(int? id)
        {
            string sql = " SELECT * FROM S_User WHERE ID=@ID";
            return base.Get<S_User>(sql, new { ID = id });
        }
        
        //删除用户
        public int DeleteUser(int? id)
        {
            string sql = " DELETE  FROM S_User WHERE ID=@ID";
            return base.Excute(sql, new { ID = id });
        }

        //验证用户名是否有重复
        public int CheckUser(string loginName)
        {
            string sql = "SELECT COUNT(ID) FROM S_User WHERE LoginName=@LoginName";

            return base.Get<int>(sql, new { LoginName = loginName });
        }

        //验证用户是否
        public S_User LoginUser(string Name, string Pwd)
        {
            string sql = " SELECT * FROM S_User WHERE LoginName=@LoginName and Password = @Password";
            return base.Get<S_User>(sql, new { LoginName = Name, Password = Pwd });
        }
    }
}
